<?php
class StringUtil {

    /**
     * 判断字符串是否为纯数字
     * @param $str
     * @return
     */
    public static function isNumeric($str){
        $str = self::delKongGe($str);
        if (preg_match("/^([0-9])+$/", $str, $match)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否包含数字
     * @param $str
     * @return
     */
    public static function isContainNumeric($str) {
        $str = self::delKongGe($str);
        if (preg_match("/([0-9])/", $str, $match)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否为纯中文
     * @param $str
     * @return
     */
    public static function isChinese($str) {
        $str = self::delKongGe($str);
        if (preg_match_all("/^([\x81-\xfe][\x40-\xfe])+$/", $str, $match)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否包含中文
     * @param $str
     * @return
     */
    public static function isContainChinese($str) {
        $str = self::delKongGe($str);
        if (preg_match("/([\x81-\xfe][\x40-\xfe])/", $str, $match)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否为纯英文
     * @param $str
     * @return
     */
    public static function isEnglish($str) {
        $str = self::delKongGe($str);
        if (preg_match_all("/^([a-zA-Z])+$/", $str, $match)) {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否包含英文
     * @param $str
     * @return
     */
    public static function isContainEnglish($str) {
        $str = self::delKongGe($str);
        if (preg_match_all("/([a-zA-Z])/", $str, $match)) {
            return true;
        }
        return false;
    }

    /**
     * 获取字符串结构
     * AA AB AAA AAB ABB AAAA AABB ABAB ABBB AAAB ABAC AABC ABCC
     * @param $str
     * @return
     */
    public static function getTrademarkStruct($str){
        $struct = [];
        $struct['id'] = 0;
        $struct['desc'] = "";
        //去空格
        $str = self::delKongGe($str);
        $length = mb_strlen($str);
        if( $length > 0 ){
            //字符串转数组
            $char = array();
            for( $i = 0; $i < $length; $i++ ){
                $char[$i] = msubstr($str, $i, 1);
            }
            switch ($length){
                case 2:
                    //'AA' => 1,'AB' => 2,
                    if( $char[0] == $char[1] ){
                        $struct['id'] = 1;
                        $struct['desc'] = "AA";
                        break;
                    }
                    $struct['id'] = 2;
                    $struct['desc'] = "AB";
                    break;
                case 3:
                    //'ABB' => 3,'AAB' => 4,'AAA' => 5,
                    if( $char[0] == $char[1] && $char[1] == $char[2] ){
                        $struct['id'] = 5;
                        $struct['desc'] = "AAA";
                    }else if( $char[0] == $char[1] && $char[1] != $char[2] ){
                        $struct['id'] = 4;
                        $struct['desc'] = "AAB";
                    }else if( $char[0] != $char[1] && $char[1] == $char[2] ){
                        $struct['id'] = 3;
                        $struct['desc'] = "ABB";
                    }
                    break;
                case 4:
                    //'AAAA' => 6, 'AABB' => 7, 'ABAB' => 8, 'ABBB' => 9, 'AAAB' => 10, 'ABAC' => 11, 'AABC' => 12, 'ABCC' => 13,
                    if( $char[0] == $char[1] && $char[1] == $char[2] && $char[2] == $char[3]){
                        $struct['id'] = 6;
                        $struct['desc'] = "AAAA";
                    }
                    if( $char[0] == $char[1] && $char[1] != $char[2] && $char[2] == $char[3] ){
                        $struct['id'] = 7;
                        $struct['desc'] = "AABB";
                    }
                    if( $char[0] == $char[2] && $char[1] == $char[3] && $char[2] != $char[3] ){
                        $struct['id'] = 8;
                        $struct['desc'] = "ABAB";
                    }
                    if( $char[0] != $char[1] && $char[1] == $char[2] && $char[2] == $char[3] ){
                        $struct['id'] = 9;
                        $struct['desc'] = "ABBB";
                    }
                    if( $char[0] == $char[1] && $char[1] == $char[2] && $char[2] != $char[3] ){
                        $struct['id'] = 10;
                        $struct['desc'] = "AAAB";
                    }
                    if( $char[0] == $char[2] && $char[1] != $char[2] && $char[2] != $char[3] ){
                        $struct['id'] = 11;
                        $struct['desc'] = "ABAC";
                    }
                    if( $char[0] == $char[1] && $char[1] != $char[2] && $char[2] != $char[3] ){
                        $struct['id'] = 12;
                        $struct['desc'] = "AABC";
                    }
                    if( $char[0] != $char[1] && $char[1] != $char[2] && $char[2] == $char[3] ){
                        $struct['id'] = 13;
                        $struct['desc'] = "ABCC";
                    }
                    break;
                default:
                    break;
            }
        }
        return $struct;
    }

    /**
     * 获取商标组成结构
     * 纯中文 纯英文 纯数字 纯图形 中文+英文 中文+数字 英文+数字
     * @param $str
     * @return
     */
    public static function getTrademarkNameCombinedType($str){
        $combinedType = array();
        $combinedType['id'] = 0;
        $combinedType['desc'] = "";
        //去空格
        $str = self::delKongGe($str);
        $length = mb_strlen($str);
        if( $length > 0 ){
            if( $str == "图形" ){
                $combinedType['id'] = 3;
                $combinedType['desc'] = "图形";
            }else{
                //是否包含中文
                $containChinese = self::isContainChinese($str);
                //是否包含英文
                $containEnglish = self::isContainEnglish($str);
                //是否包含数字
                $containNumeric = self::isContainNumeric($str);
                if( $containChinese == true && $containEnglish == false && $containNumeric == false ){
                    $combinedType['id'] = 1;
                    $combinedType['desc'] = "纯中文";
                }else if( $containChinese == false && $containEnglish == true && $containNumeric == false ){
                    $combinedType['id'] = 4;
                    $combinedType['desc'] = "纯英文";
                }else if( $containChinese == false && $containEnglish == false && $containNumeric == true ){
                    $combinedType['id'] = 2;
                    $combinedType['desc'] = "纯数字";
                }else if( $containChinese == true && $containEnglish == true && $containNumeric == false ){
                    $combinedType['id'] = 5;
                    $combinedType['desc'] = "中文+英文";
                }else if( $containChinese == true && $containEnglish == false && $containNumeric == true ){
                    $combinedType['id'] = 6;
                    $combinedType['desc'] = "中文+数字";
                }else if( $containChinese == false && $containEnglish == true && $containNumeric == true ){
                    $combinedType['id'] = 7;
                    $combinedType['desc'] = "英文+数字";
                }else if( $containChinese == true && $containEnglish == true && $containNumeric == true ){
                    $combinedType['id'] = 8;
                    $combinedType['desc'] = "中文+英文+数字";
                }
            }
        }
        return $combinedType;
    }

    /**
     * 字符串去除空格
     * @param $str
     * @return array|string|string[]
     */
    public static function delKongGe($str){
        //去空格
        $str = str_replace('　','',$str); //替换全角空格为半角空格
        $str = str_replace(' ','',$str); //替换半角空格
        return $str;
    }
}
?>
